<?php

namespace app\backend\controller\hrm;

use app\common\controller\Basic;
use app\common\service\hrm\CompanyService;
use app\common\service\hrm\DeptService;
use app\common\service\hrm\RankingService;
use app\common\service\sys\UserService;

/**
 * 人事档案
 * @author kaige
 * @update 2022-04-30
 * @edit kaige 2022-06-25
 */
class Employee extends Basic
{
  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new UserService();
  }
  /**
   * 列表
   * @return mixed
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      url_decode($data);
      return json($this->service->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 通讯录
   * @return mixed
   */
  public function tongxunlu()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      url_decode($data);
      $data['tongxunlu'] = 1;
      return json($this->service->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 获取单位公司
   * @return array
   */
  protected function getcompany()
  {
    return CompanyService::getCompanyList();
  }

  /**
   * 获取部门
   * @return array
   */
  protected function getdept()
  {
    $data = (new DeptService())->getDeptTree();
    return $data;
  }

  /**
   * 获取岗位
   * @return array
   */
  protected function getranking()
  {
    $data = (new RankingService())->getRankingTree();
    return $data;
  }

  /**
   * 获取其它信息
   */
  public function getother()
  {
    if ($this->request->isGet()) {
      $data = [
        'company' => $this->getcompany(),
        'dept' => $this->getdept()
      ];
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   * @param array $data
   */
  protected function _form_filter(&$data)
  {
    url_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
  }

  /**
   * 新增
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = $this->request->post();
      $this->_form_filter($data);
      return $this->service->saveUserForm($data);
    }
    if ($this->request->isGet()) {
      $data = [];
      $data['deptList'] = $this->getdept();
      $data['companyList'] = $this->getcompany();
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   */
  public function edit()
  {
    if ($this->request->isPost()) {
      $data = $this->request->post();
      $this->_form_filter($data);
      return $this->service->saveUserForm($data);
    }
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getInfoById($id);
      $data['deptList'] = $this->getdept();
      $data['companyList'] = $this->getcompany();
      $this->edit_out_format($data);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 编辑时输出数据格式化
   */
  protected function edit_out_format(&$data)
  {
    if (isset($data['avatar'])) {
      $data['avatar_url'] = _img($data['avatar']);
    }
    if (isset($data['contract_file'])) {
      $data['contract_file_url'] = _img($data['contract_file']);
    }
    if (isset($data['quit_file'])) {
      $data['quit_file_url'] = _img($data['quit_file']);
    }
    if (isset($data['id_card_front'])) {
      $data['id_card_front_url'] = _img($data['id_card_front']);
    }
    if (isset($data['id_card_verso'])) {
      $data['id_card_verso_url'] = _img($data['id_card_verso']);
    }
    if (isset($data['educ_img'])) {
      $data['educ_img_url'] = _img($data['educ_img']);
    }
    if (isset($data['entry_time'])) {
      $data['entry_time'] = time2str($data['entry_time'], 2);
    }
    if (isset($data['dimission_time']) && !empty($data['dimission_time'])) {
      $data['dimission_time'] = time2str($data['dimission_time'], 2);
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id', '');
      return json($this->service->_del($id));
    }
    return _jerr(1000);
  }

  /**
   * 人员分析
   */
  public function analyse()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      return json($this->service->analyse($data));
    }
    return _jerr(1000);
  }
}
